#!/bin/bash
##
#  部署脚本，包含重启或者拉取最新代码进行发布
##

# 获取输入的部署环境变量
currentModule=${1}
currentENV='test'
# 执行的jar包目录位置
currentJAR=`echo ${currentModule}/target/*.jar`
# 生产环境全局变量
#productENV='prod'
# 获取当前所在分支
#codeBranch=`git branch | grep "*"`
ck_ips='ck-ips-0.0.1'
deploy(){
    if [ -z "$currentModule" ]
    then
        echo -e "\033[31m please input witch module should be deployed !! \033[0m"
        echo "exit ...."
        exit 1
    fi

    #1 从git服务器拉取最新代码
    echo -e "\033[32m pull newest code......................... \033[0m"
    #svn update
    git pull

    #2 maven 打包
    echo -e "\033[32m packaging................................ \033[0m"
    mvn clean -pl ${currentModule} -am package -Dmaven.test.skip=true

    #3 获取当前正在运行中的 java-process-id
    appPid=`ps -ef | grep ${currentJAR} | grep -v grep | awk '{print $2}'`

    #4 kill之前的应用进程
    echo -e "\033[32m kill process ${appPid}.................. \033[0m"
    kill -9 ${appPid}

    #5 休息3秒，等待占用内存被回收
    sleep 3

    #6 清空老的nohup
    echo '' > nohup.out

    #7 deploy project now . 生产环境重新分配jvm参数设置
    echo -e "\033[32m start deploy ${currentJAR}............................... \033[0m"

    #  指定JVM内存
	if [[ ${currentJAR} == *$ck_ips* ]];then

		nohup java -server -Xms512M -Xmx1024M -jar ${currentJAR} --spring.profiles.active=${currentENV} &
	# xcm_dmp 指定JVM内存4G
	else
		# nohup java -server -jar ${currentJAR} --spring.profiles.active=${currentENV} &
        # 看板给小内存即可
        nohup java -server -Xms512M -Xmx1024M -jar ${currentJAR} --spring.profiles.active=${currentENV} &
	fi

    #8 备份此次jar包
    if [ ! -d /data/backup ];then
      mkdir -p /data/backup
    fi
   # cp ${currentJAR} /data/backup/`date +%Y%m%d%H%M%S`_${currentJAR##*/}

    echo `date +%Y%m%d%H%M%S` : publish : ${currentJAR##*/} : ${currentENV}  >> /data/publish.log
    #9 打印nohup.out日志
    tail -F nohup.out
}

restart(){
    if [ -z "$currentENV" ]
    then
        echo -e "\033[31m  please input witch env should be restart !! \033[0m"
        echo "exit ...."
        exit 1
    fi

	if [ ! -f "${currentJAR}" ]
    then
        echo -e "\033[31m  no target/*.jar, use deploy when you first publish !! \033[0m"
        echo "exit ...."
        exit 1
    fi

    #1 获取当前正在运行中的 java-process-id
    appPid=`ps -ef | grep ${currentJAR} | grep -v grep | awk '{print $2}'`

    #2 kill之前的应用进程
    echo -e "\033[32m kill process ${appPid}.................. \033[0m"
    kill -9 ${appPid}

    #3 休息3秒，等待占用内存被回收
    sleep 3

    #4 清空老的nohup
    echo '' > nohup.out

    #5 restart project now . 生产环境重新分配jvm参数设置
    echo -e "\033[32m restart ${currentJAR}............................... \033[0m"
    nohup java -server -Xms4096M -Xmx4096M -jar ${currentJAR} --spring.profiles.active=${currentENV} &

    echo `date +%Y%m%d%H%M%S` : restart : ${currentJAR##*/} : ${currentENV} >> /data/publish.log
    #6 打印nohup.out日志
    tail -F nohup.out
}

echo -e "\033[32m  *************************************************************** \033[0m"
echo ""
echo -e "\033[32m  ======== deploy env     is :  \033[33m [${currentENV}] !"
echo -e "\033[32m  ======== current module  is :  \033[33m [${currentModule}] !"

echo ""
echo -e "\033[32m  *************************************************************** \033[0m"
read -p " please make sure the message given is right! [ d ] for deploy, [ r ] for restart,the other will be exit. :" choose

if [ $choose = "d" ];then
    deploy;
elif [ $choose = "r" ];then
    restart;
else
   echo -e "\033[31m  exit ... \033[0m"
   exit 1;
fi
